#include "MyString.h"
#include <stdio.h>
#include <string.h>

void _init_my_string(_my_str *s)
{
    s->len = 0;
    memset(s->str, 0, Size);
}

// 替换原来的字符串
void _insert_str(_my_str *s, const char *_new_str)
{
    int slen = strlen(_new_str);
    s->len = Size - 1 > slen ? slen : Size - 1;
    strncpy(s->str, _new_str, s->len);
}

void _append_str(_my_str *s, const char *_new_str)
{
    strncat(s->str, _new_str, Size - 1 - s->len);
    s->len = strlen(s->str);
}

void _print_my_str(_my_str *s, bool _have_enter)
{
    if (_have_enter)
        printf("%s\n", s->str);
    else
        printf("%s", s->str);
}

void _remove_string(_my_str *s, const char *str)
{
    char *dest = strstr(s->str, str);
    while (dest != NULL)
    {
        char *back = dest + strlen(str);
        while (*back != '\0')
        {
            *dest++ = *back++;
        }
        *dest = '\0';
        dest = strstr(s->str, str);
    }
    s->len = strlen(s->str);
}

bool _is_str_equal(_my_str *s, _my_str *str)
{
    return strcmp(s->str, str->str) == 0;
}
// char* GetSPtr(MyStr *s)
// {
//     return s->str;
// }

void _get_str(_my_str *s)
{
    _init_my_string(s);
    char c = 0;
    //读到回车结束
    while( (c = getchar())!= '\n' && s->len < Size - 1)
    {
        s->str[s->len++] = c;
    }
}

